﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using RuleCode.Table;

namespace map.member.lvyou
{
    public partial class newviewpoint : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitMap();
            if (!IsPostBack)
            {
                Bind0();
            }

        }
        private void Bind0()
        {
            string sheng = zfunction.Province;
            RuleCode.Table.县 x = RuleCode.Table.县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList1.Items.Clear();
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            // DropDownList7.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList1.Items.Add(item["县编码"].ToString());
            }

            if (x.Count > 0)
            {
                Bind1();
            }
            else
            {
                //todo:用geocode显示该县
                string provice = zfunction.Province;
                string county = DropDownList1.SelectedValue;
                string query = provice + "," + county;
                string scriptTag = "showAddress('" + query + "');";
                ScriptManager.RegisterStartupScript(
                this.UpdatePanel1, this.UpdatePanel1.GetType(), "bodyevent3", scriptTag, true);
            }
        }
        /// <summary>
        /// 绑定区
        /// </summary>
        private void Bind1()
        {
            string xian = DropDownList1.Text;
            RuleCode.Table.区 q = RuleCode.Table.区.Find(" 县编码 = '" + xian + "' ");
            DropDownList2.Items.Clear();
            DropDownList3.Items.Clear();
            //DropDownList7.Items.Clear();
            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList2.Items.Add(item["区编码"].ToString());
            }
            if (q.Count > 0)
            {
                Bind2();
            }
            else
            {
                RuleCode.Table.区 q1 = RuleCode.Table.区.Find(" 区编码 = '" + DropDownList2.Text + "' ");
                markpos2(q1.taobaolat.ToString(), q1.taobaolong.ToString(), q1.区编码.ToString());

            }
        }
        /// <summary>
        /// 绑定区域
        /// </summary>
        private void Bind2()
        {
            RuleCode.Table.区域 q = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
               + " and 县编码 = '" + DropDownList1.Text + "' ");
            DropDownList3.Items.Clear();

            foreach (DataRow item in q.MyTable.Rows)
            {
                DropDownList3.Items.Add(item["区域编码"].ToString());
            }
            //if (q.Count > 0)
            //{
            //    Bind4();
            //}


            markpos2(q.taobaolat.ToString(), q.taobaolong.ToString(), q.区域编码.ToString());

            GridViewBind();
        }
        private void InitMap()
        {
            string ScriptTag = "";


            ScriptTag =
                "<script type=\"text/javascript\">markpos(" + "116.39679908752441" + ","
                + "" + "39.917043746896674,'" + "2222" + "');</script>";

            ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
        }
        /// <summary>
        /// 县
        /// </summary>
        private void bind1()
        {
            string sheng = zfunction.Province;
            RuleCode.Table.县 x = RuleCode.Table.县.Find(" 省编码 = '" + sheng + "' ");
            DropDownList1.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList1.Items.Add(item["县编码"].ToString());
            }
            string ScriptTag = "";
            ScriptTag =
                    "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                    + "" + "39.917043746896674,'" + "2" + "');</script>";
            //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
            ScriptManager.RegisterStartupScript(
              this.Page, this.Page.GetType(), "bodyevent3", ScriptTag, false);
            //DropDownList1.DataBind();
        }
        /// <summary>
        /// 区
        /// </summary>
        private void bind2()
        {
            string xian = DropDownList1.Text;
            RuleCode.Table.区 x = RuleCode.Table.区.Find(" 县编码 = '" + xian + "' ");
            DropDownList2.Items.Clear();
            foreach (DataRow item in x.MyTable.Rows)
            {
                DropDownList2.Items.Add(item["区编码"].ToString());

            }
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    string provice = zfunction.Province;
                    string county = DropDownList1.SelectedValue;
                    string query = provice + "," + county;
                    ScriptTag = "showAddress('" + query + "');";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区编码.ToString() + "');</script>";
                }
                //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
                ScriptManager.RegisterStartupScript(
            this.Page, this.Page.GetType(), "bodyevent3", ScriptTag, false);
                markpos2(x.taobaolat.ToString(), x.taobaolong.ToString(), x.区编码.ToString());
            }
        }

        /// <summary>
        /// 区域表格
        /// </summary>
        private void bind3()
        {
            RuleCode.Table.区域 x = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
                + " and 县编码 = '" + DropDownList1.Text + "' ");
            DropDownList3.DataSource = x.MyTable.DefaultView;
            DropDownList3.DataTextField = "区域编码";
            DropDownList3.DataValueField = "区域编码";
            DropDownList3.DataBind();
            if (!IsPostBack)
            {
                string ScriptTag = "";
                if (x.taobaolong.Value == 0 || x.taobaolat.Value == 0)
                {
                    //ScriptTag =
                    //    "<script type=\"text/javascript\">markpos(" + "116.396" + ","
                    //    + "" + "39.917,'" + x.区编码.ToString() + "');</script>";
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + x.区域编码.ToString() + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + x.taobaolong.ToString() + ","
                        + x.taobaolat.ToString() + ",'" + x.区域编码.ToString() + "');</script>";
                }
                //ClientScript.RegisterStartupScript(GetType(), "bodyevent", ScriptTag);
                //    ScriptManager.RegisterStartupScript(
                //this.Page, this.Page.GetType(), "bodyevent3", ScriptTag, false);
                markpos2(x.taobaolat.ToString(), x.taobaolong.ToString(), x.区域编码.ToString());
            }

        }


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind1();
            Bind2();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            Bind2(); ;
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            //BizArea uc = BizArea.Find(" recid = '" + id + "'", "RECID");
            //uc.Delete();
            //GridViewBind();
        }

        protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string id = GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
            lyviewpoint uc = lyviewpoint.Find(" recid = '" + id + "'", "RECID");

            TextBox1.Text = uc.lyviewpointid.ToString();
            TextBox_long.Text = uc.taobaolong.ToString();
            TextBox_lat.Text = uc.taobaolat.ToString();
            Label_recid.Text = uc.RECID.ToString();
            string ScriptTag = "";
            if (uc.taobaolong.Value == 0 || uc.taobaolat.Value == 0)
            {
                ScriptTag =
                    "markpos2(" + "" + "116.39679908752441,"
                    + "39.917043746896674" + ",'" + uc.lyviewpointid.ToString() + "')";
            }
            else
            {
                ScriptTag =
                    "markpos2(" + uc.taobaolong.ToString() + ","
                    + uc.taobaolat.ToString() + ",'" + uc.lyviewpointid.ToString() + "')";
            }
            //ScriptManager.RegisterStartupScript(
            //    this.UpdatePanel1, this.UpdatePanel1.GetType(), "bodyevent3", ScriptTag, true);
            markpos2(uc.taobaolat.ToString(), uc.taobaolong.ToString(), uc.lyviewpointid.ToString());
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (DropDownList1.Text != "" && DropDownList2.Text != "")
            {
                lyviewpoint uc = lyviewpoint.Find(" 1=2 ");
                uc.New();
                uc.区编码.Value = DropDownList2.Text;
                uc.县编码.Value = DropDownList1.Text;
                uc.区域编码.Value = DropDownList3.Text;
                uc.lyviewpointid.Value = TextBox1.Text;
                uc.valid.Value = RuleCode.Enum. NoYes.No;
                //todo:change after finished login function
                //finished
                uc.createuserid = AliApi.appUserId;

                uc.taobaolong.Value = zfunction.str2double(TextBox_long.Text);
                uc.taobaolat.Value = zfunction.str2double(TextBox_lat.Text);
                uc.Insert();
                GridViewBind();
            }
            else
            {
                MessageBox.Show(this.UpdatePanel1, "操作不成功.");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            lyviewpoint x = lyviewpoint.Find(" recid = '" + Label_recid.Text + "'", "RECID");
            x.lyviewpointid.Value = TextBox1.Text;
            x.taobaolong.Value = zfunction.str2double(TextBox_long.Text);
            x.taobaolat.Value = zfunction.str2double(TextBox_lat.Text);
            x.Modify();
            GridViewBind();
        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
        {
            RuleCode.Table.区域 q = RuleCode.Table.区域.Find(" 区编码 = '" + DropDownList2.Text + "' "
                  + " and 县编码 = '" + DropDownList1.Text + "' " + " and 区域编码 = '" + DropDownList3.Text + "' ");

            markpos2(q.taobaolat.ToString(), q.taobaolong.ToString(), q.区域编码.ToString());

            GridViewBind();
        }
        //changed:只能看到自己的，只能创建，不能删除，可修改
        private void GridViewBind()
        {
            RuleCode.Table.lyviewpoint x = RuleCode.Table.lyviewpoint.Find(" 区编码 = '" + DropDownList2.Text + "' "
                                                                   + " and 县编码 = '" + DropDownList1.Text + "' " +
                                                                   " and 区域编码 = '" + DropDownList3.Text + "' " +
                                                                   " and createuserid = '" + AliApi.appUserId + " '");
            this.GridView1.DataSource = x.MyTable;
            this.GridView1.DataBind();
        }

        private void markpos2(string taobaolat, string taobaolong, string name)
        {
            string ScriptTag = "";
            if (!IsPostBack)
            {

                if (taobaolong == "0" || taobaolat == "0")
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + "116.39679908752441" + ","
                        + "" + "39.917043746896674,'" + name + "');</script>";
                }
                else
                {
                    ScriptTag =
                        "<script type=\"text/javascript\">markpos2(" + taobaolong + ","
                        + taobaolat + ",'" + name + "');</script>";
                }
                ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
                    "messagew", ScriptTag, false);
            }
            else
            {
                //改变了值的情况、
                ScriptTag = "markpos2(" + taobaolong + ","
                          + taobaolat + ",'" + name + "');";

                ScriptManager.RegisterStartupScript(
                this.UpdatePanel1, this.UpdatePanel1.GetType(), "bodyevent3", ScriptTag, true);
            }

        }
    }
}
